package com.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: sun.hongliang
 * @create: 2019/11/18 17:44
 */
public class IsIsomorphic {
    public static void main(String[] args) {
        System.out.println(isIsomorphic("ab", "ca"));
    }

    public static boolean isIsomorphic(String s, String t) {
        Map<Character, Character> map = new HashMap<>();
        Map<Character, Character> mapR = new HashMap<>();
        char[] sChars = s.toCharArray();
        char[] tChars = t.toCharArray();
        for (int i = 0; i < sChars.length; i++) {
            char sChar = sChars[i];
            char tChar = tChars[i];
            Character sCharMap = map.get(sChar);
            Character tCharMap = mapR.get(tChar);
            if (sCharMap != null && sCharMap != tChar) {
                return false;
            }
            if (tCharMap != null && tCharMap != sChar) {
                return false;
            }
            map.put(sChar,tChar);
            mapR.put(tChar,sChar);
        }
        return true;
    }
}
